package com.qst.mapreduce.wordcount.shiyanwu;

import com.alibaba.fastjson2.JSONObject;
import org.apache.http.HttpEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

import java.io.IOException;

public class IpToProvince {

    public static void main(String[] args) throws Exception {
        getProvince("124.135.40.160");
    }
    public static String getProvince(String ip) throws IOException {
        String url = "https://qifu-api.baidubce.com/ip/geo/v1/district?ip="+ip;
        CloseableHttpClient httpClient = HttpClients.createDefault();
        HttpGet httpGet = new HttpGet(url);
        CloseableHttpResponse response = httpClient.execute(httpGet);
        // 获取响应实体
        HttpEntity entity = response.getEntity();
        String result = EntityUtils.toString(entity, "UTF-8");
        JSONObject data = JSONObject.parseObject(result).getJSONObject("data");
        String province = data.getString("prov");
        System.out.println(province);
        return province;
    }
}